package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmplyeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.ClassinfoService;
import cn.wolfcode.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {

    @Autowired
    private ClassinfoService classinfoService;

    @Autowired
    private EmployeeService  employeeService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") EmplyeeQueryObject qo, Model model){
        List<Employee> empls = employeeService.selectAll();
        PageInfo<Classinfo> pageResult = classinfoService.query(qo);
        model.addAttribute("empls",empls);
        model.addAttribute("pageResult",pageResult);
        return "classinfo/list";
    }

/*
    @RequestMapping("/input")
    public  String input(Long id,Model model){
        if(id != null){
            Classinfo classinfo = classinfoService.selectByPrimaryKey(id);
            model.addAttribute("classinfo",classinfo);
        }
        return "classinfo/input";
    }*/

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            classinfoService.deleteByPrimaryKey(id);
        }
        return new JsonResult();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Classinfo classinfo, Long empId){
        Employee employee = new Employee();
        employee.setId(empId);
        classinfo.setEmployee(employee);

        if(classinfo.getId() !=null){
            classinfoService.updateByPrimaryKey(classinfo);
        }else {
            classinfoService.insert(classinfo);
        }
        return new JsonResult();
    }


}
